require.config({
    paths:{
        "jquery":"../lib/jquery.min",
        "mode":"module"
    }
})

require(['jquery'],function($){
    $(function(){
        require(['mode'],function(myMode){
            myMode.baseMode()
           //这里写本页面专用的js
            //全选按钮
            $('.catalist_all').click(function(){
                location.href = './goodlist.html' + myMode.info()
            })
            //购物袋按钮 -- 跳转页面功能
            $('.bag').click(function(){
                location.href = './cart.html' + myMode.info()
            })
            //登录框登录按钮
            $('#logo').click(function(){
                location.href = './' + myMode.info()
            })
            

            //这里写本页专用代码
            //监听其他页面更改localstorage,自动刷新，
            window.addEventListener("storage", function () { 
                location.reload(); 
            })
            //加载数据
            $.ajax({
                url:"./data/goods.json",
                type:"get",
                dataType:"json",
                success:function(data){   //data就是请求到的整个json文件
                    let goods = data["goods"] //取出全部商品
                    let allgoods = ''
                    $.each(goods,function(attr,item){
                        // console.log(attr,item);
                        $.each(item,function(index,obj){
                            // console.log(obj["title"])
                            // 通过a标签传数据也太能作了(*/ω＼*)
                            let temp = `
                                    <li class="base_up">
                                    <div class="img_outer">
                                    <a href="./good.html?id=${obj["id"]}&price=${obj["price"]}&title=${obj["title"]}&m-title=${obj["m-title"]}&o-price=${obj["o-price"]}`+myMode.info()+`"><img src=${obj["src"]} alt=""></a>
                                    </div>
                                    <div class="good_dsc"><h2>${obj["title"]}</h2>
                                    <h3>${obj["m-title"]}</h3>
                                    <p>￥${obj["price"]}<del>￥${obj["o-price"]}</del></p>
                                    <button data-id=${obj.id}>加入购物车</button>
                                    </div>
                                </li>
                            `
                            allgoods+=temp
                        })
                    })
                    $('.allGoods_inner').html(allgoods)
                }
            })

            if(localStorage.getItem('goods')){
                var goodArr = JSON.parse( localStorage.getItem('goods') )
            }else{
                var goodArr = []
            }
            //商品添加购物车功能
            $('.allGoods_inner').on("click","button",function(){ //将每个按钮的点击事件委托给父级
                let id = $(this).attr('data-id')
                let flag = false
                $.each(goodArr,function(index,item){
                    if(item.id == id){
                        item.num++
                        flag = true
                    }
                })
                if(!flag){
                    goodArr.push({'id':id,'num':1,'check':true})
                }
                localStorage.setItem('goods',JSON.stringify(goodArr))
                alert("添加成功")
            })

         })
        
    })
})







    
